﻿using System;
using System.IO; 
using System.Collections.Generic;
using System.Web;

public class cmpEntry
{
   public int cmpID; 
   public string cmpName;

   public string cmpCountry;  // new 
   public string cmpAddress;
   public string cmpCity;     // new 
   public string cmpPhone;    // new  
   public string cmpFax;      // new 
   public string cmpWebsite;  // new 

   public string cmpEmail;
   public string cmpLogoLink; 

   public String cmpDescription;
   public String cmpKeywords; 
    
   public HttpPostedFile cmpLogofile;

   public string app_path;

   public int cmpOwner; 
   public string cmpCreatedDate; 

   private byte[] image;
   private string image_type; 

   public string current_user; 
                
   public cmpEntry()
   {
        cmpName = "";
        cmpAddress = "";
        cmpEmail = "";
        cmpCountry = "";
        cmpCity = "";
        cmpPhone = "";
        cmpFax = "";
        cmpWebsite = "";  

        cmpDescription = "";
        cmpKeywords = ""; 

        cmpLogoLink=""; 
        cmpLogofile = null; 

        image = null;        
    }

   enum CMP_DATA_ACTION_TYPE { CMP_POST_NEW, CMP_UPDATE_EXIST };  

   public bool cmpPost()
    {
        try
        {
            dbProcessor my_db = new dbProcessor();

            int result_id;

            my_db.insert_entry(cmpName, cmpCountry, cmpCity, cmpAddress, cmpPhone, cmpFax, cmpWebsite, cmpEmail, current_user, out result_id);

            cmpID = result_id;

            my_db.sq.Close();
            
        }
        catch(Exception E)        
        { 
            return false;
        }
     
        if (!(cmpLogofile.FileName==""))
        {
            cmpPostImage(CMP_DATA_ACTION_TYPE.CMP_POST_NEW); 
        } 
           

        if ((cmpDescription != "") || (cmpKeywords != ""))
        {
           
                dbProcessor my_activity_data = new dbProcessor();
                my_activity_data.insert_activity_data(cmpID, cmpDescription, cmpKeywords);
                my_activity_data.sq.Close();           
        }

        return true; 
    }

   private void cmpPostImage(CMP_DATA_ACTION_TYPE action)
    {      

            byte[] stored_img = new byte[cmpLogofile.ContentLength];

            cmpLogofile.InputStream.Read(stored_img, 0, cmpLogofile.ContentLength);

            MemoryStream mem_strm = new MemoryStream(stored_img, 0, cmpLogofile.ContentLength); 

            dbProcessor my_image_db = new dbProcessor();

            imageProcessor my_img = new imageProcessor();

            image = mem_strm.ToArray();
        
            image_type = my_img.get_image_ext(cmpLogofile);

            switch (action)
            { 
                case CMP_DATA_ACTION_TYPE.CMP_POST_NEW:
                    my_image_db.insert_image(cmpID, image, image_type);
                    break; 

                case CMP_DATA_ACTION_TYPE.CMP_UPDATE_EXIST:
                    my_image_db.update_image(cmpID, image, image_type); 
                    break; 
                
                default:
                    break; 
            }

            my_image_db.insert_small_image(cmpID, image); 
            my_image_db.sq.Close();
    }
          
   public void cmpGet(int ID)
    {
        dbProcessor my_db = new dbProcessor();
        my_db.select_entry(ID, out cmpName, out cmpCountry, out cmpCity, out cmpAddress, out cmpPhone, out cmpFax, out cmpEmail, out cmpWebsite, out cmpDescription, out cmpLogoLink, out cmpOwner, out cmpCreatedDate);
        
        cmpID = ID; // Add checking on NULL 
    }

   public void cmpUpdate()
    {
        dbProcessor my_db = new dbProcessor();
        
        my_db.update_entry(cmpID, cmpName, cmpAddress, cmpEmail, cmpPhone, cmpFax, cmpCountry, cmpCity, cmpWebsite);

       if (!(cmpLogofile==null))
       {
        cmpPostImage(CMP_DATA_ACTION_TYPE.CMP_UPDATE_EXIST);
       }
    }

   public void cmpDelete()
    {
        dbProcessor my_db = new dbProcessor();
 
        my_db.delete_entry(cmpID);
        my_db.delete_activity_data(cmpID);
        my_db.delete_image(cmpID);
       
               
    }
}